package com.sth.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sth.base.exceptionHandler.SthException;
import com.sth.edu.entity.Chapter;
import com.sth.edu.entity.Video;
import com.sth.edu.entity.chapter.ChapterVo;
import com.sth.edu.entity.chapter.VideoVo;
import com.sth.edu.mapper.ChapterMapper;
import com.sth.edu.service.ChapterService;
import com.sth.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author sth
 * @since 2020-12-05
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    //课程大纲列表,根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 1根据课程id 查询所有章节
        QueryWrapper<Chapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<Chapter> chapterList = baseMapper.selectList(wrapperChapter);
        // 2根据课程id 查询所有小节
        QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<Video> videoList = videoService.list(wrapperVideo);

        // 创建list集合,用于存储最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        // 3封装
        for (int i = 0; i < chapterList.size(); i++) {
            Chapter chapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapterList.get(i), chapterVo);

            finalList.add(chapterVo);
            // 4.遍历小节进行封装
            List<VideoVo> videoList1 = new ArrayList<>();
            if(!CollectionUtils.isEmpty(videoList)) {
                for (int m = 0; m < videoList.size(); m++) {
                    Video video = videoList.get(m);
                    if (video.getChapterId().equals(chapter.getId())) {
                        VideoVo videoVo = new VideoVo();
                        BeanUtils.copyProperties(video, videoVo);
                        videoList1.add(videoVo);
                    }
                }
            }
            chapterVo.setChildren(videoList1);
        }
        return finalList;
    }

    // 删除章节的方法
    @Override
    public Boolean deleteChapter(String chapterId) {
        // 根据chapterId章节id查询数据,不能进行删除
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        if (count > 0) {// 查询出小节,
            throw new SthException(20001, "不能删除");
        } else {
            // 删除章节
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }

    }
    //根据课程id删除章节

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.deleteById(wrapper);
    }

}
